﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Data;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;





namespace GrouponDesktop
{
    class Funciones
    {


        public static string NuevoCodigoUnico(int maxSize)
        {
            char[] chars = new char[62];
            chars =
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
            byte[] data = new byte[1];
            RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
            crypto.GetNonZeroBytes(data);
            data = new byte[maxSize];
            crypto.GetNonZeroBytes(data);
            StringBuilder result = new StringBuilder(maxSize);
            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length)]);
            }
            return result.ToString();
        }




        public static byte[] ObtenerHash(string cadena)
        {
            //Utilizamos el algoritmo 256
            SHA256 shaM = new SHA256Managed();

            //Convertimos un texto plano en un array de byte
            byte[] dato = System.Text.Encoding.UTF8.GetBytes(cadena);

            //Calculo el hash de mi texto plano
            return (shaM.ComputeHash(dato));
        }


        public static string ImprimirHash(string cadena)
        {
            //Obtenemos Hash
            byte[] result = ObtenerHash(cadena);

            string x = null;
            //Mostremos el hash en si  
            for (int i = 0; i < result.Length - 1; i++)
            {
                x = x + result[i].ToString("x2");
                //Console.WriteLine(result[i].ToString());//.ToString("X2"));
            }
            return x;
        }


    }
}
